

package com.bei_en.teacher.admin.controller.message;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.message.bean.Message;
import com.bei_en.teacher.admin.model.message.condition.MessageCondition;
import com.bei_en.teacher.admin.model.message.vo.MessageVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.message.MessageService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-9-10 0:10:23
 */
@Controller
@RequestMapping("/api/Users")
public class MessageAPPController extends BaseController {

    @Autowired
    MessageService messageService;


    @RequestMapping("/message")
	@ResponseBody
    public JsonResult message(Model model,@Valid  MessageCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<MessageVo> list =new ArrayList<MessageVo>();
		   list = messageService.findMessagePage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   MessageVo messageVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   messageVo=list.get(i);
	   object.put("id",  messageVo.getId());
	   object.put(
				"content",
				""
						+ "<p align=\"center\"><span style=\"font-size:18px;\"><strong>"
						+ messageVo.getTitle()
						+ "</strong></span></p><p><span style=\"color:#CCCCCC;\">时间:  "
						+ "  </span></p><p></p>"
						+ messageVo.getContent());
	   object.put("title",  messageVo.getTitle());
	   object.put("createTime",  messageVo.getCreateTime().getTime());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/messageAdd")
    @ResponseBody
    public JsonResult messageAdd(Model model,@Valid  MessageCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		
      	
		    MessageVo message=new MessageVo();
		    
	   message.setId(vo.getId());
	   message.setContent(vo.getContent());
	   message.setTitle(vo.getTitle());
//	   message.setCreateTime(vo.getCreateTime());
		    
			messageService.insert(message);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
    @RequestMapping("/messageEdit")
    public JsonResult messageEdit(Model model,@Valid  MessageCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	MessageVo message=new MessageVo();
	
		messageService.updateSelective(message);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
